/**
 *
 */
package com.rf.richfitwheel.admin.sys.service.impl;

import com.rf.richfitwheel.admin.sys.dao.StationMapper;
import com.rf.richfitwheel.admin.sys.dao.UserStationMapper;
import com.rf.richfitwheel.admin.sys.model.Station;
import com.rf.richfitwheel.admin.sys.model.StationExample;
import com.rf.richfitwheel.admin.sys.model.UserStation;
import com.rf.richfitwheel.admin.sys.model.UserStationExample;
import com.rf.richfitwheel.admin.sys.service.SysUserStationService;
import com.rf.richfitwheel.common.contants.Constant;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhanglu
 *
 */
@Service("sysUserStationService")
@ComponentScan({"com.rf.richfitwheel.admin.sys.dao"})
public class SysUserStationServiceImpl implements SysUserStationService {

    @Resource
    private UserStationMapper userStationMapper;
    @Resource
    private StationMapper stationMapper;

    @Override
    public List<UserStation> getListByUserId(String userId) {
        UserStationExample example = new UserStationExample();
        UserStationExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserStation> userStations = userStationMapper.selectByExample(example);
        return userStations;
    }

    @Override
    public List<UserStation> getListByUserIdAndOrgId(String userId, String orgId) {
        UserStationExample example = new UserStationExample();
        UserStationExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andOrgIdEqualTo(orgId);
        List<UserStation> userStations = userStationMapper.selectByExample(example);
        return userStations;
    }
    
    @Override
    public List<UserStation> getListByUserIdAndOrgId(String userId, String orgId, String moduleType) {
        StationExample stationExample = new StationExample();
        StationExample.Criteria stationCriteria = stationExample.createCriteria();
        List<String> moduleTypeList = Arrays.stream(moduleType.split(",")).collect(Collectors.toList());
        moduleTypeList.add(Constant.SystemModule.sys.name());
        stationCriteria.andModuleTypeIn(moduleTypeList);
        stationCriteria.andOrgIdEqualTo(orgId);
        List<Station> stationList = stationMapper.selectByExample(stationExample);
        UserStationExample example = new UserStationExample();
        UserStationExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andOrgIdEqualTo(orgId);
        List<UserStation> userStations = new ArrayList<>();
        if(!stationList.isEmpty()){
            criteria.andStationIdIn(stationList.stream().map(Station::getId).collect(Collectors.toList()));
            userStations = userStationMapper.selectByExample(example);
        }
        return userStations;
    }

    @Override
    public void deleteByUserIdAndOrgId(String userId, String orgId) {
        List<UserStation> userStations = this.getListByUserIdAndOrgId(userId, orgId);
        for (UserStation userStation : userStations) {
            userStationMapper.deleteByPrimaryKey(userStation.getId());
        }
    }

    @Override
    public void deleteByUserId(String userId) {
        UserStationExample example = new UserStationExample();
        UserStationExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserStation> userStations = userStationMapper.selectByExample(example);
        for (UserStation userStation : userStations) {
            userStationMapper.deleteByPrimaryKey(userStation.getId());
        }
    }

    @Override
    public List<String> getStationIdsByUserId(String userId) {
        List<String> stationIds = new ArrayList<>();
        List<UserStation> userStations = this.getListByUserId(userId);
        for (UserStation userStation : userStations) {
            stationIds.add(userStation.getStationId());
        }
        return stationIds;
    }
    
    /**
     * 只删除对应模块的记录
     *
     * @param userId     用户名
     * @param moduleType 模块
     */
    @Override
    public void deleteByUserIdAndModuleType(String userId, String moduleType){
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        List<String> modList = Arrays.stream(moduleType.split(",")).collect(Collectors.toList());
        modList.add(Constant.SystemModule.sys.name());
        params.put("moduleType", modList);
        userStationMapper.deleteByUserIdAndModuleType(params);
    }
}
